package calorie.com.calorie.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;


import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import calorie.com.calorie.R;
import calorie.com.calorie.adapter.NoticeListAdapter;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.NoticeListContact;
import calorie.com.calorie.response.NoticeListResponce;
import retrofit2.Response;

/**
 * 公告列表
 */
public class NoticeListActivity extends MvpActivity<NoticeListContact.NoticeListPresent> implements NoticeListContact.INoticeListView {
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;

    List<NoticeListResponce.DataBean> mList;
    NoticeListAdapter mAdapter;
    int pageNo = 1;

    @Override
    public void initData(Bundle savedInstanceState) {
        mList = new ArrayList<>();
        mAdapter = new NoticeListAdapter(R.layout.recy_item_notice_list, mList);

        LinearLayoutManager manager = new LinearLayoutManager(context);
        rv.setLayoutManager(manager);
        rv.setAdapter(mAdapter);


        mPresent.getNoticeList(context, pageNo);

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNo = 1;
                mPresent.getNoticeList(context, pageNo);
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNo++;
                mPresent.getNoticeList(context, pageNo);
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_notice_list;
    }


    @Override
    public void getNoticeListData(Response<NoticeListResponce> response) {
        refresh.finishRefresh();
        refresh.finishLoadMore();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            List<NoticeListResponce.DataBean> data = response.body().getData();
            if (pageNo == 1) {
                mList.clear();
            }
            mList.addAll(data);
            if(mList.size()>0){
                mList.get(0).setIsMAx(1);
            }

            mAdapter.notifyDataSetChanged();

            if (!ObjectUtils.isEmpty(data) && data.size() < 10) {
                refresh.setEnableLoadMore(false);
            } else {
                refresh.setEnableLoadMore(true);
            }
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected NoticeListContact.NoticeListPresent createPresent() {
        return new NoticeListContact.NoticeListPresent();
    }
}
